package com.xyht.sca_s.student_manage_system.common.util;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xyht.sca_s.student_manage_system.modules.org.entity.OrgNode;
import com.xyht.sca_s.student_manage_system.modules.org.entity.SmsOrgStructure;
import com.xyht.sca_s.student_manage_system.modules.org.entity.SmsOrgUserRelation;
import com.xyht.sca_s.student_manage_system.modules.org.mapper.SmsOrgStructureMapper;
import com.xyht.sca_s.student_manage_system.modules.org.mapper.SmsOrgUserRelationMapper;
import com.xyht.sca_s.student_manage_system.modules.org.util.NodeUtil;
import com.xyht.sca_s.student_manage_system.modules.student.entity.SmsStudentInfo;
import com.xyht.sca_s.student_manage_system.modules.student.entity.SmsUserStudentRelation;
import com.xyht.sca_s.student_manage_system.modules.student.mapper.SmsStudentInfoMapper;
import com.xyht.sca_s.student_manage_system.modules.student.mapper.SmsUserStudentRelationMapper;
import com.xyht.sca_s.student_manage_system.modules.user.entity.SmsUser;
import com.xyht.sca_s.student_manage_system.modules.user.mapper.SmsUserMapper;
import com.xyht.sca_s.student_manage_system.modules.welcomeNewStudent.entity.resp.StuOrgInfoResp;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Component
public class OrgUserStuUtil {

    @Resource
    private SmsOrgStructureMapper smsOrgStructureMapper;
    @Resource
    private SmsOrgUserRelationMapper smsOrgUserRelationMapper;
    @Resource
    private SmsUserStudentRelationMapper smsUserStudentRelationMapper;
    @Resource
    private SmsUserMapper smsUserMapper;
    @Resource
    private SmsStudentInfoMapper smsStudentInfoMapper;

    /**
     * 通过userId获取stuId
     *
     * @param userId userId
     * @return stuId/null
     */
    public String getStuIdByUserId(String userId) {
        List<SmsUserStudentRelation> userStudentRelationList = smsUserStudentRelationMapper.selectList(new LambdaQueryWrapper<SmsUserStudentRelation>()
                .eq(SmsUserStudentRelation::getUserId, userId));
        if (userStudentRelationList.isEmpty()) {
            return null;
        }

        return userStudentRelationList.get(0).getStuId();
    }

    /**
     * 通过 stuId 获取 userId
     *
     * @param stuId stuId
     * @return userId/null
     */
    public String getUserIdByStuId(String stuId) {
        List<SmsUserStudentRelation> userStudentRelationList = smsUserStudentRelationMapper.selectList(new LambdaQueryWrapper<SmsUserStudentRelation>()
                .eq(SmsUserStudentRelation::getStuId, stuId));
        if (userStudentRelationList.isEmpty()) {
            return null;
        }

        return userStudentRelationList.get(0).getUserId();
    }

    /**
     * 根据用户id获取学生组织信息
     *
     * @param userId 用户id
     * @return 学生组织信息, 失败返回null
     */
    public StuOrgInfoResp getStuOrgInfoByUserId(@NotNull String userId) {
        SmsUser user = smsUserMapper.selectById(userId);
        if (user == null) {
            return null;
        }

        List<SmsUserStudentRelation> userStudentRelationList = smsUserStudentRelationMapper.selectList(new LambdaQueryWrapper<SmsUserStudentRelation>()
                .eq(SmsUserStudentRelation::getUserId, userId));
        if (userStudentRelationList.isEmpty()) {
            return null;
        }

        SmsStudentInfo studentInfo = smsStudentInfoMapper.selectById(userStudentRelationList.get(0).getStuId());
        if (studentInfo == null) {
            return null;
        }

        List<SmsOrgUserRelation> userRelationList = smsOrgUserRelationMapper.selectList(new LambdaQueryWrapper<SmsOrgUserRelation>()
                .eq(SmsOrgUserRelation::getUserId, userId));
        if (userRelationList.isEmpty()) {
            return null;
        }

        SmsOrgStructure classOrg = smsOrgStructureMapper.selectById(userRelationList.get(0).getOrgId());
        if (classOrg == null || !classOrg.getLevel().equals("6")) {
            return null;
        }

        SmsOrgStructure gradeOrg = smsOrgStructureMapper.selectById(classOrg.getParentId());
        if (gradeOrg == null) {
            return null;
        }

        SmsOrgStructure majorOrg = smsOrgStructureMapper.selectById(gradeOrg.getParentId());
        if (majorOrg == null) {
            return null;
        }

        StuOrgInfoResp stuOrgInfoResp = new StuOrgInfoResp();
        stuOrgInfoResp.setStuId(studentInfo.getId());
        stuOrgInfoResp.setClassId(String.valueOf(classOrg.getId()));
        stuOrgInfoResp.setClassName(classOrg.getOrgName());
        stuOrgInfoResp.setGradeId(String.valueOf(gradeOrg.getId()));
        stuOrgInfoResp.setGradeName(gradeOrg.getOrgName());
        stuOrgInfoResp.setMajorId(String.valueOf(majorOrg.getId()));
        stuOrgInfoResp.setMajorName(majorOrg.getOrgName());

        return stuOrgInfoResp;
    }

    /**
     * 根据orgId获取某组织下的所有userId
     *
     * @param orgId orgId
     * @return userId集合
     */
    public List<String> getUserIdListByOrgId(Integer orgId) {
        List<String> userIdList = new ArrayList<>();
        if (orgId == null) {
            return userIdList;
        }

        // 获取全部组织
        List<SmsOrgStructure> orgList = smsOrgStructureMapper.selectList(new QueryWrapper<>());
        List<OrgNode> orgNodeList = orgList.stream().map(orgStructure -> {
            OrgNode orgNode = new OrgNode();
            BeanUtils.copyProperties(orgStructure, orgNode);
            return orgNode;
        }).collect(Collectors.toList());
        // 获取当前年级下所有userId
        List<Integer> childIdList = NodeUtil.getChildNodes(orgNodeList, orgId);
        List<SmsOrgUserRelation> userRelationList = smsOrgUserRelationMapper.selectList(new LambdaQueryWrapper<SmsOrgUserRelation>()
                .in(SmsOrgUserRelation::getOrgId, childIdList));
        if (userRelationList.isEmpty()) {
            return userIdList;
        }

        // 收集去重
        userIdList = userRelationList.stream().map(SmsOrgUserRelation::getUserId).distinct().collect(Collectors.toList());

        return userIdList;
    }

    /**
     * 根据orgId获取所有stuId
     *
     * @param orgId orgId
     * @return stuId集合
     */
    public List<String> getStuIdListByOrgId(Integer orgId) {
        List<String> stuIdList = new ArrayList<>();

        List<String> userIdList = getUserIdListByOrgId(orgId);
        if (userIdList.isEmpty()) {
            return stuIdList;
        }

        return getStuIdListByUserIdList(userIdList);
    }

    /**
     * 根据userId获取所有orgId
     *
     * @param userId userId
     * @return orgId集合
     */
    public List<String> getOrgIdListByUserId(String userId) {
        List<String> orgIdList = new ArrayList<>();

        List<SmsOrgUserRelation> userRelationList = smsOrgUserRelationMapper.selectList(new LambdaQueryWrapper<SmsOrgUserRelation>()
                .eq(SmsOrgUserRelation::getUserId, userId));
        if (userRelationList.isEmpty()) {
            return orgIdList;
        }

        orgIdList = userRelationList.stream().map(SmsOrgUserRelation::getOrgId).distinct().collect(Collectors.toList());

        return orgIdList;
    }

    /**
     * 根据userId集合获取stuId集合
     *
     * @param userIdList userId集合
     * @return stuId集合
     */
    public List<String> getStuIdListByUserIdList(List<String> userIdList) {
        List<String> stuIdList = new ArrayList<>();
        if (userIdList.isEmpty()) {
            return stuIdList;
        }

        List<SmsUserStudentRelation> userStudentRelationList = smsUserStudentRelationMapper.selectList(new LambdaQueryWrapper<SmsUserStudentRelation>()
                .in(SmsUserStudentRelation::getUserId, userIdList));
        if (userStudentRelationList.isEmpty()) {
            return stuIdList;
        }

        stuIdList = userStudentRelationList.stream().map(SmsUserStudentRelation::getStuId).distinct().collect(Collectors.toList());

        return stuIdList;
    }

    /**
     * 根据stuId集合获取userId集合
     *
     * @param stuIdList stuId集合
     * @return userId集合
     */
    public List<String> getUserIdListByStuIdList(List<String> stuIdList) {
        List<String> userIdList = new ArrayList<>();
        if (stuIdList.isEmpty()) {
            return userIdList;
        }

        List<SmsUserStudentRelation> userStudentRelationList = smsUserStudentRelationMapper.selectList(new LambdaQueryWrapper<SmsUserStudentRelation>()
                .in(SmsUserStudentRelation::getStuId, stuIdList));
        if (userStudentRelationList.isEmpty()) {
            return userIdList;
        }

        userIdList = userStudentRelationList.stream().map(SmsUserStudentRelation::getUserId).distinct().collect(Collectors.toList());

        return userIdList;
    }

    /**
     * 根据模糊查询字符串查询符合条件的stuIdList
     *
     * @param searchStr 模糊查询字符串
     * @return stuIdList
     */
    public List<String> getStuIdListBySearchStr(String searchStr) {
        List<String> stuIdList = new ArrayList<>();

        List<SmsStudentInfo> studentInfoList = smsStudentInfoMapper.selectList(new LambdaQueryWrapper<SmsStudentInfo>()
                .and(i -> i.like(SmsStudentInfo::getStuNo, searchStr).or()
                        .like(SmsStudentInfo::getStuName, searchStr).or()
                        .like(SmsStudentInfo::getStuTel, searchStr).or()
                        .like(SmsStudentInfo::getStuIdCard, searchStr).or()
                        .like(SmsStudentInfo::getStuSex, searchStr).or()
                        .like(SmsStudentInfo::getStuCollege, searchStr).or()
                        .like(SmsStudentInfo::getStuMajor, searchStr).or()
                        .like(SmsStudentInfo::getStuGrade, searchStr).or()
                        .like(SmsStudentInfo::getStuClass, searchStr).or()
                        .like(SmsStudentInfo::getStuNation, searchStr).or()
                        .like(SmsStudentInfo::getStuNativePlace, searchStr)));
        if (!studentInfoList.isEmpty()) {
            stuIdList = studentInfoList.stream().map(SmsStudentInfo::getId).collect(Collectors.toList());
        }

        return stuIdList;
    }


}
